package topin.facecat.Product.UI;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import java.util.*;
import facecat.topin.core.*;
import topin.facecat.Android.*;
import topin.facecat.MainActivity;

public class LinkControl extends FCView {
    public LinkControl()
    {
        setBorderColor(FCColor.None);
        setBackColor(FCColor.None);
    }

    public FCUIView m_relateView;

    public boolean m_mustHide = false;

    public void onRemove(){
        stopTimer(m_timerID);
        super.onRemove();
    }

    public FrameLayout.LayoutParams params4;

    public void delete()
    {
        if (m_relateView != null){
            ((ViewGroup)m_relateView.getParent()).removeView(m_relateView);
            m_relateView.delete();
            m_relateView = null;
        }
        super.delete();
    }

    public boolean m_isClickRunning;

    public String m_target = "";

    /// <summary>
    /// 获取目标名称
    /// </summary>
    /// <returns></returns>
    public String getTarget()
    {
        return m_target;
    }

    private FrameLayout.LayoutParams params3;

    public static boolean m_loadX5;

    /// <summary>
    /// 设置目标名称
    /// </summary>
    /// <param name="value"></param>
    public void setTarget(String value) {
        AndroidHost androidHost = (AndroidHost) getNative().getHost();
        FrameLayout frameLayout = new FrameLayout(MainActivity.m_mainActivity);
        m_target = value;
        /*m_relateView = new FCUIVIew(MainActivity.m_mainActivity);
        m_relateView.setVisibility(View.INVISIBLE);
        params3 = new FrameLayout.LayoutParams
                (FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        MainActivity.m_mainActivity.addContentView(m_relateView, params3);*/
    }

    public String m_redirectFlag1 = "";

    public int m_timerID = FCView.getNewTimerID();

    public void onAdd()
    {
        super.onAdd();
        startTimer(m_timerID, 100);
    }

    private FCPoint m_lastPoint = new FCPoint(-100, -100);

    public void onTimer(int timerID)
    {
        super.onTimer(timerID);
        if (timerID == m_timerID)
        {
            if (m_relateView != null)
            {
                if(isPaintVisible(this) && !m_mustHide){
                    if(getWidth() > 0 && getHeight() > 0) {
                        if(m_relateView.getVisibility() == View.INVISIBLE) {
                            m_relateView.setVisibility(View.VISIBLE);
                        }
                    }
                }else {
                    m_relateView.setVisibility(View.INVISIBLE);
                }
            }
            if (m_lastPoint.x != pointToNative(new FCPoint(0, 0)).x
                    || m_lastPoint.y != pointToNative(new FCPoint(0, 0)).y)
            {
                update();
                m_lastPoint = pointToNative(new FCPoint(0, 0));
            }
        }
    }

    /// <summary>
    /// 获取属性值
    /// </summary>
    /// <param name="name">属性名称</param>
    /// <param name="value">属性值</param>
    /// <param name="type">类型</param>
    public void getAttribute(String name, RefObject<String> value, RefObject<String> type)
    {
        if (name.equals("target"))
        {
            type.argvalue = "text";
            value.argvalue = getTarget();
        }
        else if (name.equals("redirectflag1"))
        {
            type.argvalue = "text";
            value.argvalue = m_redirectFlag1;

        }
        else
        {
            super.getAttribute(name, value, type);
        }
    }

    /// <summary>
    /// 获取属性列表
    /// </summary>
    /// <returns>属性列表</returns>
    public ArrayList<String> getAttributeNames()
    {
        ArrayList<String> attributeNames = super.getAttributeNames();
        attributeNames.add("Target");
        attributeNames.add("RedirectFlag1");
        return attributeNames;
    }

    public String m_addingTarget = "";

    /// <summary>
    /// 设置属性
    /// </summary>
    /// <param name="name">属性名称</param>
    /// <param name="value">属性值</param>
    public void setAttribute(String name, String value)
    {
        if (name.equals("target"))
        {
            m_addingTarget = value;
        }
        else if (name.equals("redirectflag1"))
        {
            String newUrl = value;
            m_redirectFlag1 = newUrl;
        }
        else
        {
            super.setAttribute(name, value);
        }
    }

    private static boolean m_init = false;

    /// <summary>
    /// 是否初始化
    /// </summary>
    /// <returns></returns>
    public static boolean isInit()
    {
        return m_init;
    }

    private FCRect m_lastRect = new FCRect();

    /// <summary>
    /// 设置初始化
    /// </summary>
    /// <param name="init"></param>
    public static void setInit(boolean init)
    {
        m_init = init;
    }

    /// <summary>
    /// 布局改变方法
    /// </summary>
    public void update()
    {
        super.update();
        try {
            int width = getWidth(), height = getHeight();
            if (width > 0 && height > 0 && m_addingTarget.length() > 0) {
                String dealTarget = m_addingTarget;
                m_addingTarget = "";
                setTarget(dealTarget);
            }
            if(width < 0){
                width = 0;
            }
            if(height < 0){
                height = 0;
            }
            if (width >= 0 && height >= 0 && m_relateView != null) {
                FCPoint nativePoint1 = pointToNative(new FCPoint(0, 0));
                FCPoint nativePoint2 = new FCPoint(nativePoint1.x + getWidth(), nativePoint1.y + getHeight());
                double scaleFactorX = 1, scaleFactorY = 1;
                RefObject<Double> refScaleFactorX = new RefObject<Double>(scaleFactorX);
                RefObject<Double> refScaleFactorY = new RefObject<Double>(scaleFactorY);
                getNative().getPaint().getScaleFactor(refScaleFactorX, refScaleFactorY);
                scaleFactorX = refScaleFactorX.argvalue;
                scaleFactorY = refScaleFactorY.argvalue;
                int nativeWidth = m_native.getSize().cx, nativeHeight = m_native.getSize().cy;
                if (m_init) {
                    nativePoint1.x = (int) (nativePoint1.x / scaleFactorX);
                    nativePoint1.y = (int) (nativePoint1.y / scaleFactorY);
                    nativePoint2.x = (int) (nativePoint2.x / scaleFactorX);
                    nativePoint2.y = (int) (nativePoint2.y / scaleFactorY);
                    nativeWidth = (int) (nativeWidth / scaleFactorX);
                    nativeHeight = (int) (nativeHeight / scaleFactorX);
                } else {
                    nativePoint1.x = (int) (nativePoint1.x * scaleFactorX);
                    nativePoint1.y = (int) (nativePoint1.y * scaleFactorY);
                    nativePoint2.x = (int) (nativePoint2.x * scaleFactorX);
                    nativePoint2.y = (int) (nativePoint2.y * scaleFactorY);
                    nativeWidth = (int) (nativeWidth * scaleFactorX);
                    nativeHeight = (int) (nativeHeight * scaleFactorX);
                }
                if (nativePoint2.x > nativePoint1.x && nativePoint2.y > nativePoint1.y) {
                    FCRect nowRect = new FCRect(nativePoint1.x, nativePoint1.y, nativeWidth - nativePoint2.x, nativeHeight - nativePoint2.y);
                    if (nowRect.left != m_lastRect.left || nowRect.top != m_lastRect.top || nowRect.right != m_lastRect.right || nowRect.bottom != m_lastRect.bottom) {
                        params3.setMargins(nowRect.left, nowRect.top, nowRect.right, nowRect.bottom);
                        m_relateView.setLayoutParams(params3);
                    }
                    m_lastRect = nowRect;
                }else{
                    if(width == 0 || height == 0){
                        m_relateView.setVisibility(View.INVISIBLE);
                    }
                }
            }
        }catch (Exception ex){
        }
    }
}
